<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>优惠券使用</title>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="validator" uri="http://www.springmodules.org/tags/commons-validator" %>
<core:set var ="webcontext" scope="request" value="${pageContext.request.contextPath}"/>
<core:set var ="imagepath" scope="request" value="${webcontext}/images"/>
<core:set var ="csspath" scope="request" value="${webcontext}/css"/>
<core:set var ="jspath" scope="request" value="${webcontext}/javascript"/>
<core:set var ="jsppath" scope="request" value="${webcontext}/jsp"/>
<core:set var ="htmlpath" scope="request" value="${webcontext}/html"/>
<core:set var ="bootstrapPath" scope="request" value="${webcontext}/bootstrap"/>
<core:set var ="layerPath" scope="request" value="${webcontext}/layer"/>
<!-- TODO the gdms url should replace by the token -->
<core:set var ="gdmsurl" scope="request" value="@token-gdms.server.servlet.context.url.integration@"/>
<script type="text/javascript">
	var contextPath = "${webcontext}";
</script>
<%@page import="com.cap.bts.framework.common.util.Pager"%>
<%@page import="com.cap.bts.framework.common.constant.CommonConstants"%>
<%@page import="com.cap.bts.framework.common.util.CodeTextUtils"%>
<%@page import="com.cap.bts.framework.common.util.CookieUtils"%>
<%@page import="com.cap.bts.framework.auth.constant.AuthConstant"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<link rel="stylesheet" type="text/css" href="${csspath }/style_l.css" />
<script type="text/javascript" src="${jspath }/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${layerPath}/layer.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>
<link rel="stylesheet" type="text/css"	href="${csspath}/jquery.alerts.css" />
<script type="text/javascript" src="${jspath}/load_layer.js"></script>
<link rel="stylesheet" type="text/css"	href="${webcontext}/css/validationEngine.jquery.css" />
<script src="${jspath}/jquery/validate/jquery.validationEngine.js"	type="text/javascript"></script>
<script src="${jspath}/jquery/validate/jquery.validationEngine-zh.js" type="text/javascript"></script>
<%-- <link rel="stylesheet" type="text/css"	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script> --%>

<style type="text/css">
* {
	margin: 0px;
	padding: 0px;
	font-family: "Microsoft YaHei";
	color: #000;
}

body{min-width:600px;}

html,body {
	font: 12px "Microsoft YaHei", "Microsoft YaHei", Verdana, Arial,
		sans-serif;
	color: #000;
	overflow:auto;
}
.form-label{text-align: right; padding-right: 15px; width:200px;}
.input-price{height:25px;width:200px;font-weight:bold;font-size:16px;}
select{height:25px;}

.redemptionTable{margin-left:50px; width:700px;}
.redemptionTable td{line-height:30px;}
.redemptionTable tr{border-bottom:1px solid #CCC;}

</style>
<script type="text/javascript">

$(function(){
	$("#packageNumberTxt").on("keypress", function(event){
		if(13 == event.keyCode){
			return false;
		}
		
	});
});


function checkNumber(){
	
	var couponNumber = $.trim($("#packageNumberTxt").val());
	if(couponNumber.length == 0){
		alert("请输入优惠券号码");
		return;
	}
	$("#msg").html("正在获取优惠券信息...");
	$('#couponNameMsg').html("");
	$("#redemptionBox").html("");
	
	$.ajax({
		type : 'POST',
		dataType:'json',
		url : '${webcontext}/MARS/couponInfo',
		async : true,
		data : "couponNumber=" + couponNumber,
		dataType : 'json',
		success : function(data) {
			var html = "";
			if(data.success){
				html = data.data.batchName + "" + data.data.ticketName;
				g_deductionAmount = data.data.deductionAmount;
				if(data.data.deductionMethod == "PERCENT"){
					g_deductionMethod = "1";
				}else{
					g_deductionMethod = "0";
				}
				
				g_couponNumber = couponNumber;
				
				loadRedemptionPage(data.data.ticketDefId);
			}else{
				html = data.message;
			}
			$('#msg').html("");
			$('#couponNameMsg').html(html);
			
		},
		error: function(){
			$('#msg').html("数据加载失败！");
			//layer.alert("数据加载失败！", {icon : 2});
		}
	});
	
}

function loadRedemptionPage(ticketDefId){
	$.ajax({
		type : 'POST',
		dataType:'json',
		url : '${webcontext}/MARS/coupon/ticket/' + ticketDefId,
		async : false,
		//data : "couponNumber=" + couponNumber,
		dataType : 'html',
		success : function(data) {
			
			$('#redemptionBox').html(data);
			
		},
		error: function(){
			$('#redemptionBox').html("数据加载失败，请重试！");
			//layer.alert("数据加载失败！", {icon : 2});
		}
	});
}
var g_itemIndex = 0;
function toItemTypeInfoList(itemId,ticketId,ticketLineId){
	var loadIndex = layer.load(1, {
	    shade: [0.5,'#000'],
		offset : ['250px', '450px']
	});
	$.ajax({
		type : 'POST',
		url : "${webcontext}/MARS/toWecareItemTypeInfoList2?itemId="+itemId+"&ticketId="+ticketId+"&ticketLineId="+ticketLineId+"&serverCenter=${param.serverCenter}",//iframe的url
		async : true,
		dataType : 'html',
		success : function(data) {
			layer.close(loadIndex);
			
			if(data.indexOf("系统错误，请联系系统管理员") > 0){
				layer.alert("请求失败,请重试！", {
				    icon: 2,
				    offset : ['250px', '350px']
				});
				
				return;
			}
			//iframe层
		g_itemIndex =	layer.open({
			    type: 1,
			    title: '商品列表',
			    scrollbar:true,
			    shadeClose: false,
			    move: false,
			    closeBtn: 1,
			    shade: 0.1, 
			    offset : ['10px', '100px'],
			    area: ['800px', '90%'],
			    //content: "${webcontext}/MARS/toWecareItemTypeInfoList?itemId="+itemId+"&ticketId="+ticketId+"&packageId="+packageId+"&serverCenter="+$("#dealerIdHtxt").val(),//iframe的url
			    content: "<div id='itemBox'>" + data + "</div>",//iframe的url
			    end: function(){ 
			    	//关闭弹出层后回调方法
			    }
			}); 
		},
		error : function(data) {
			layer.close(loadIndex);
			layer.alert("请求失败,请重试！", {
			    icon: 2,
			    offset : ['250px', '350px']
			});
		}
	});
	
}

function submitData(){
	var gData = {
			"memberId": ${memberInfo.id},
			"carId": ${carPlateInfo.id},
			"couponNo": g_couponNumber,
			"soNumber": "${param.soNumber}",
			"discountType": g_deductionMethod,
			"serverCenter": "${param.serverCenter}",
	};
	
	var data = new Array();
	
	$(".description-input").each(function(index, item){
		//console.log($(item).val());
		var itemObj = $(item);
		
		var tempItem = {
			"itemType" : 	itemObj.attr("itemType"),	
			"itemNo" : 	itemObj.attr("itemNo"),	
			"itemSource" : 	itemObj.attr("itemSource"),	
			"quantity" : 	itemObj.attr("quantity"),	
			"discount" : 	g_deductionAmount,	
			"description" : $("#description_" + itemObj.attr("itemId")).text()
		}
		
		data.push(tempItem);
	});
	gData.itemList = data;
	
	$("#msg").html("正在保存数据，请稍等...");
	
	setTimeout(function(){
		$.ajax({
			type : 'POST',
			dataType:'json',
			url : '${webcontext}/MARS/coupon/redemption',
			contentType : "application/json;charset=utf-8",
			async : false,
			data : JSON.stringify(gData),
			success : function(data) {
				var html = "";
				if(data.success){
					
					$("#submitDataBtn").remove();
					html = "操作成功，请关闭页面！";
					
				}else{
					html = data.message;
				}
				
				$('#msg').html(html);
				
			},
			error: function(){
				$('#msg').html("数据加载失败！");
				//layer.alert("数据加载失败！", {icon : 2});
			}
		});
	}, 400);
	
}

var g_deductionAmount=0,g_couponNumber='',g_deductionMethod='',g_discount="";
</script>

</head>
<body>

			<form:form id="edit_purchase" action="#" method="post"
				modelAttribute="submitDto" enctype="multipart/form-data">
				<div class="data_div">
					<table class="data_tab">
						<tr>
							<th class="form-label">手机</th>
							<td>
							<input type="hidden" id="dealerIdHtxt"
								name="dealerId" value="${memberInfo.dealerId}">
							<input type="hidden" id="memberNameHtxt"
								name="memberId" value="${memberInfo.id}">
							<input type="hidden" id="memberNameHtxt"
								name="memberName" value="${memberInfo.memberName}">
								<input type="hidden" id="mobilePhoneHtxt" name="mobilePhone"
								value="${memberInfo.mobilePhone}"> <input
								type="hidden" id="carPlateTHtxt" name="carPlate"
								value="${memberInfo.carPlate}"> <input
								type="hidden" id="priceHtxt" name="price"> <input
								type="hidden" id="isPhysicalCardHtxt" name="isPhysicalCard" /> <input
								type="hidden" id="isPrepaymentCardHtxt" name="isPrepaymentCard">
								<input type="hidden" id="unitOfPeriodHtxt" name="unitOfPeriod">
								<input type="hidden" id="validPeriodHtxt" name="validPeriod">
								<input type="hidden" id="onhandQuantityHtxt"
								name="onhandQuantity"> <input type="hidden"
								id="cardDefinitionIdHtxt" name="cardDefinition.id">
								${memberInfo.mobilePhone}</td>
						</tr>
						<tr>
							<th class="form-label">姓名</th>
							<td>${memberInfo.memberName}</td>
						</tr>
						<tr>
							<th class="form-label">车牌</th>
							<td>${memberInfo.carPlate}</td>
						</tr>
						<tr>
							<th class="form-label">订单编号</th>
							<td><input type="hidden" id="soNumberHtxt"
								name="soNumberSale" value="${memberInfo.soNumber}">${memberInfo.soNumber}</td>
						</tr>
						
						<tr>
							<th class="form-label">优惠券号</th>
							<td>
							<input id="packageNumberTxt" name="packageNumber"
								class="form-control input-price"
								value="">
							<button type="button" class="btn" style="width: 80px; height: 30px" 
							onclick="checkNumber()">验证券号</button>
							</td>
						</tr>
					</table>
					<div style="margin:20px 200px;font-weight:bold;"><span id="couponNameMsg" style="color: red;"></span></div>
					<div id="redemptionBox" style="margin:20px;"></div>
					<div style="margin-left:100px;"><span id="msg" style="color: red;"></span></div>
				</div>
			</form:form>



</body>

</html>